
from datetime import datetime
from flask_login import UserMixin
from sqlalchemy import DateTime, Numeric

from werkzeug.security import generate_password_hash, check_password_hash
from applications.exts import db


# # 商品表
class Goods_Remark(db.Model,UserMixin):
    __tablename__ = 'goods_remark'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='评论表id')
    openid = db.Column(db.String(60), db.ForeignKey('wx_user.openid'),nullable=False,  comment='微信用户外建')
    gid = db.Column(db.Integer, db.ForeignKey('wx_goods.id'),nullable=False,  comment='商品外建')
    content = db.Column(db.String(255), nullable=False, comment='评论内容')
    start = db.Column(db.Integer,comment='星级')
    def __str__(self):
        return self.id

    def to_dict(self):
        res = {}
        for col in self.__table__.columns:
            if isinstance(col.type, DateTime):  # 判断类型是否为DateTime
                if not getattr(self, col.name):  # 判断实例中该字段是否有值
                    value = ""
                else:  # 进行格式转换
                    value = getattr(self, col.name).strftime("%Y-%m-%d %H:%M:%S")
            elif isinstance(col.type, Numeric):  # 判断类型是否为Numeric
                value = float(getattr(self, col.name))  # 进行格式转换
            else:  # 剩余的直接取值
                value = getattr(self, col.name)
            res[col.name] = value
        return res